package com.haibei.view;

import java.util.ArrayList;

import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;

import com.common.BaseActivity;
import com.haibei.R;
import com.haibei.adapter.AnimationAdapter;
import com.haibei.adapter.WheelCityAdapter;
import com.haibei.adapter.bean.City;
import com.haibei.view.wheel.ArrayWheelAdapter;
import com.haibei.view.wheel.OnWheelChangedListener;
import com.haibei.view.wheel.OnWheelScrollListener;
import com.haibei.view.wheel.WheelView;

public class LocationWheelView {

	private WheelView mWheelViewProvince;
	private WheelView mWheelViewCity;

	private StageSelectListener mSelectListener;
	private BaseActivity context;
	private ArrayList<City> mArrayListFirstLevelCity;
	private boolean scrolling = false;
	private View contentView;

	public LocationWheelView(BaseActivity context, ArrayList<City> citys) {
		this.context = context;
		this.mArrayListFirstLevelCity = citys;
		init();
	}

	private void init() {
		contentView = View.inflate(this.context, R.layout.wheel_province_city, null);
		mWheelViewProvince = (WheelView) contentView.findViewById(R.id.wheelViewProvince);
		mWheelViewCity = (WheelView) contentView.findViewById(R.id.wheelViewCity);
		
		City[] items = new City[mArrayListFirstLevelCity.size()];
		for (int i = 0, isize = mArrayListFirstLevelCity.size(); i < isize; i++) {
			items[i] = mArrayListFirstLevelCity.get(i);
		}
		WheelCityAdapter wca = new WheelCityAdapter(context, items);
		mWheelViewProvince.setViewAdapter(wca);
		mWheelViewProvince.addChangingListener(new OnWheelChangedListener() {
			@Override
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				// buildSubCitys(mArrayListFirstLevelCity.get(newValue));
				if (!scrolling) {
					buildSubCitys(mArrayListFirstLevelCity.get(newValue));
				}
			}
		});

		
		mWheelViewCity.addChangingListener(new OnWheelChangedListener() {
			@Override
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
	
			}
		});

		mWheelViewProvince.addScrollingListener(new OnWheelScrollListener() {
			public void onScrollingStarted(WheelView wheel) {
				scrolling = true;
			}

			public void onScrollingFinished(WheelView wheel) {
				scrolling = false;
				buildSubCitys(mArrayListFirstLevelCity.get(mWheelViewProvince.getCurrentItem()));
			}
		});

		contentView.findViewById(R.id.cancel).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				slideInout(contentView, false, context);
			}
		});
		contentView.findViewById(R.id.submit).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				slideInout(contentView, false, context);
				if (null != mSelectListener) {
					int pi = mWheelViewProvince.getCurrentItem();
					City city = mArrayListFirstLevelCity.get(pi).getSubCity().get(mWheelViewCity.getCurrentItem());
					mSelectListener.onSelect(city);
				}
			}
		});
	}

	private void buildSubCitys(City subCity) {
		ArrayList<City> subLsit = subCity.getSubCity();
		String[] items = new String[subLsit.size()];
		for (int i = 0, isize = subLsit.size(); i < isize; i++) {
			items[i] = subLsit.get(i).getName();
		}
//		WheelCityAdapter wca = new WheelCityAdapter(context, items);
		ArrayWheelAdapter<String> adapter = new ArrayWheelAdapter<String>(context, items);
		mWheelViewCity.setViewAdapter(adapter);
		mWheelViewCity.setCurrentItem(items.length / 2);
		
	}

	public void show() {
		slideInout(contentView, true, context);
	}

	private static void slideInout(final View view, boolean inOut, Context context) {
		if (inOut) {
			// 出来
			Animation slideIn = AnimationUtils.loadAnimation(context, R.anim.slide_bottom_in);
			slideIn.setAnimationListener(new AnimationAdapter() {
				@Override
				public void onAnimationStart(Animation animation) {
					view.setVisibility(View.VISIBLE);
				}
			});
			view.startAnimation(slideIn);
		} else {
			Animation slideOut = AnimationUtils.loadAnimation(context, R.anim.slide_bottom_out);
			slideOut.setAnimationListener(new AnimationAdapter() {
				@Override
				public void onAnimationEnd(Animation animation) {
					view.setVisibility(View.GONE);
				}
			});
			view.startAnimation(slideOut);
		}
	}

	public static interface StageSelectListener {
		public void onSelect(City city);
	}

	public StageSelectListener getSelectListener() {
		return mSelectListener;
	}

	public void setSelectListener(StageSelectListener mSelectListener) {
		this.mSelectListener = mSelectListener;
	}

	public View getContentView() {
		return this.contentView;
	}

}
